package com.zmt.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zmt.content.mapper.CourseTeacherMapper;
import com.zmt.content.po.CourseTeacher;
import com.zmt.content.service.CourseTeacherService;
import com.zmt.xuechengplusbase.exception.xuechengplusException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CourseTeacherServiceImpl implements CourseTeacherService {
    @Autowired
    private CourseTeacherMapper courseTeacherMapper;

    /**
     * 查询一门课程的老师信息
     * @param courseId
     * @return
     */
    @Override
    public List<CourseTeacher> listCourseTeacher(Long courseId) {
        if (courseId==null){
            xuechengplusException.cast("参数有问题哦");
        }
        LambdaQueryWrapper<CourseTeacher> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CourseTeacher::getCourseId,courseId);
        List<CourseTeacher> courseTeachers = courseTeacherMapper.selectList(wrapper);
        return courseTeachers;
    }

    @Override
    public CourseTeacher createCourseTeacher(CourseTeacher courseTeacher) {
        //数据校验交给JSR303去做吧
        //根据有无id来判断进行新增还是修改
        Long id = courseTeacher.getId();
        if (id == null){
            int insert = courseTeacherMapper.insert(courseTeacher);
            if (insert<=0){
                xuechengplusException.cast("添加教师失败");
            }
        }else {
            int i = courseTeacherMapper.updateById(courseTeacher);
            if (i<=0){
                xuechengplusException.cast("修改信息失败");
            }
        }
        //插入时似乎会将ID直接返回给原参
        return courseTeacher;
    }

    @Override
    public void deleteCourseTeacher(Long courseId, Long id) {
        LambdaQueryWrapper<CourseTeacher> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CourseTeacher::getCourseId,courseId).eq(CourseTeacher::getId,id);
        int count = courseTeacherMapper.delete(wrapper);
        if (count<=0){
            xuechengplusException.cast("删除失败");
        }
    }
}
